import os

from django.db import transaction
from celery_tasks.main import app
from apps.goods.models import Goods

@app.task(bind=True)
def update_goods_stock(self, id, quantity):
    """　更新MySQL商品库存　"""
    with transaction.atomic():
        # 锁定记录并更新
        goods = Goods.objects.select_for_update().get(id=id)
        goods.stock -= quantity  # 或复杂计算逻辑
        goods.save()
        print("同步缓存成功")
    return {id: goods.stock}